<?php

namespace App\Http\Controllers\admin;

use App\Dao\UsersDao;
use App\Dao\VipSettingsDao;
use App\Exceptions\SysExceptions;
use App\Model\AreaModel;
use App\Model\UsersPhoneLogModel;
use App\Services\Code;
use Psr\SimpleCache\InvalidArgumentException;

/**
 * Created by PhpStorm.
 * @purpose 用户控制器
 * @Author: admin
 * @Time: 2023/5/14 16:04
 */
class UsersController extends BaseController
{

    public function users(){
        if(getIsHasPage()){
            return Code::message('',200,UsersDao::get_user_page(request()->all()));
        }
        return view('admin.user.user');
    }

    /**
     * @throws SysExceptions
     */
    public function set_shop_user(){
        if(isPost()){
            $params = request()->all();
            UsersDao::set_shop_user($params);
            return  Code::message('',200);
        }
        $info = [
            'shop_name'=>'',
            'user_id'=>'',
            'img_url'=>'',
            'province_id'=>0,
            'city_id'=>0,
            'area_id'=>0,
            'shop_address'=>'',
            'address_po'=>''
        ];
        $user_id = request('user_id');
        $shop_query = UsersDao::get_shop_builder([
            'user_id'=>$user_id
        ]);
        if($shop_query->exists()){
            $info = $shop_query->first()->toArray();
        }
        $province = AreaModel::query()->where('parentid',0)->get();
        $city = [];
        if ($info['province_id'] != 0){
            $city = AreaModel::query()->where('parentid',$info['province_id'])->get();
        }

        $area = [];
        if ($info['city_id'] != 0){
            $area = AreaModel::query()->where('parentid',$info['city_id'])->get();
        }

        return view('admin.user.set_shop_user',[
            'user_id'=>request('user_id'),
            'info'=>$info,
            'province'=>$province,
            'city'=>$city,
            'area'=>$area
        ]);
    }



    public function get_area(){
        $parentid = request('parentid');
        return Code::message('',200,AreaModel::query()->where('parentid',$parentid)->get());
    }



    public function set_hehuo(): array
    {
        UsersDao::set_hehuo(['user_id'=>request('id')]);
        return Code::message('成功',200);
    }



    public function user_edit(){
        if(isPost()){
            $params = request()->all();
            UsersDao::update_nickname_headimgurl($params);
            return  Code::message('',200);
        }
        $userInfo = UsersDao::getUserInfo([
            'user_id'=>request('user_id')
        ]);
        return view('admin.user.user_edit',[
            'user_info'=>$userInfo
        ]);
    }


    public function chongzhi(){
        if(isPost()){
            UsersDao::moneyChongzhi(request()->all());
            return Code::message('',200);
        }
        return view('admin.user.chongzhi',[
            'user_id'=>request('user_id')
        ]);
    }


}
